package com.quanyan.club.operate.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.club.entity.db.TbOperateLotteryPrizeRecord;
import com.quanyan.club.operate.service.LotteryService;
import com.quanyan.club.web.BaseController;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.redisCluster.config.RedisTemplate;
import com.quanyan.redisCluster.utils.RedisKey;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhuo.shi on 2016/12/9.
 */
@RestController
@RequestMapping("/api/club/operate/lottery")
public class LotteryController extends BaseController{

    @Autowired
    LotteryService lotteryService;
    @Autowired
    RedisTemplate redisTemplate;

    public static final Logger logger = LoggerFactory.getLogger(LotteryController.class);

    @RequestMapping(value = "/queryLotteryPrizes/2", method = RequestMethod.POST)
    public APIResponse queryLotteryPrizeItems2(HttpServletRequest request){
        Integer uid = getUid(request, true);
        return lotteryService.queryLotteryPrizeItems2(uid);
    }

    @RequestMapping(value = "/drawLottery/2", method = RequestMethod.POST)
    public APIResponse drawLottery2(HttpServletRequest request){
        int uid = getUid(request, true);

        RedisKey redisKey = new RedisKey(Constants.CLUB_SYSTEM, "lotterylimit2" + uid);
        if (redisTemplate.exists(redisKey)){
            return APIResponse.returnFail(105, "抽奖中...");
        }else {
            redisTemplate.set(redisKey, uid);
        }
        try {
            APIResponse response = lotteryService.drawLottery2(uid);
            redisTemplate.delteKey(redisKey);
            return response;
        }catch (Exception e){
            logger.error("抽奖异常：" + e.getMessage());
            redisTemplate.delteKey(redisKey);
            return APIResponse.returnFail(100, "抽奖活动未开始");
        }
    }

    @RequestMapping(value = "/queryHitRecords/2", method = RequestMethod.POST)
    public APIResponse queryHitRecordList2(){
        return APIResponse.returnSuccess(
                lotteryService.queryHitRecordList2());
    }

    @RequestMapping(value = "/queryLotteryPrizes", method = RequestMethod.POST)
    public APIResponse queryLotteryPrizeItems(HttpServletRequest request){
        Integer uid = getUid(request, true);
        return lotteryService.queryLotteryPrizeItems(uid);
    }

    @RequestMapping(value = "/queryLotteryPrizes/newyear", method = RequestMethod.POST)
    public APIResponse queryLotteryPrizeItemsNewYear(HttpServletRequest request){
        Integer uid = getUid(request, true);
        return lotteryService.queryLotteryPrizeItemsNewYear(uid);
    }

    @RequestMapping(value = "/drawLottery", method = RequestMethod.POST)
    public APIResponse drawLottery(HttpServletRequest request){
        int uid = getUid(request, true);

        RedisKey redisKey = new RedisKey(Constants.CLUB_SYSTEM, "lotterylimit" + uid);
        if (redisTemplate.exists(redisKey)){
            return APIResponse.returnFail(105, "抽奖中...");
        }else {
            redisTemplate.set(redisKey, uid);
        }
        try {
            APIResponse response = lotteryService.drawLottery(uid);
            redisTemplate.delteKey(redisKey);
            return response;
        }catch (Exception e){
            logger.error("抽奖异常：" + e.getMessage());
            redisTemplate.delteKey(redisKey);
            return APIResponse.returnFail(100, "抽奖活动未开始");
        }
    }

    /**
     * 新年大转盘抽奖
     */
    @RequestMapping(value = "/drawLottery/newyear", method = RequestMethod.POST)
    public APIResponse drawLotteryForNewYear(HttpServletRequest request){
        int uid = getUid(request, true);

        RedisKey redisKey = new RedisKey(Constants.CLUB_SYSTEM, "lotterylimit" + uid);
        if (redisTemplate.exists(redisKey)){
            return APIResponse.returnFail(105, "抽奖中...");
        }else {
            redisTemplate.set(redisKey, uid);
        }
        try {
            APIResponse response = lotteryService.drawLotteryNewYear(uid);
            redisTemplate.delteKey(redisKey);
            return response;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("抽奖异常：" + e.getMessage());
            redisTemplate.delteKey(redisKey);
            return APIResponse.returnFail(100, "抽奖活动未开始");
        }
    }

    /**
     * 老带新抽奖
     */
    @RequestMapping(value = "/drawLottery/pullclub", method = RequestMethod.POST)
    public APIResponse drawLotteryForPullClub(HttpServletRequest request, @RequestBody Map map){
        int uid = getUid(request, true);

        RedisKey redisKey = new RedisKey(Constants.CLUB_SYSTEM, "lotterylimit" + uid + map.get("newClubId"));
        if (redisTemplate.exists(redisKey)){
            return APIResponse.returnFail(105, "抽奖中...");
        }else {
            redisTemplate.set(redisKey, uid);
        }
        try {
            APIResponse response = lotteryService.drawLotteryPullNewClub(uid, MapUtils.getIntValue(map, "newClubId"));
            redisTemplate.delteKey(redisKey);
            return response;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("抽奖异常：" + e.getMessage());
            redisTemplate.delteKey(redisKey);
            return APIResponse.returnFail(100, "抽奖活动未开始");
        }
    }

    @RequestMapping(value = "/queryHitRecords", method = RequestMethod.POST)
    public APIResponse queryHitRecordList(){
        return APIResponse.returnSuccess(
                lotteryService.queryHitRecordList());
    }

    @RequestMapping(value = "/queryHitRecords/pullclub", method = RequestMethod.POST)
    public APIResponse queryHitRecordListPullClub(HttpServletRequest request){
        List<TbOperateLotteryPrizeRecord> recordList = new ArrayList<>();
        try {
             recordList = lotteryService.queryHitRecordListPullNewClub(getUid(request, true));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return APIResponse.returnSuccess(recordList);
    }

    @RequestMapping(value = "/queryHitRecords/newyear", method = RequestMethod.POST)
    public APIResponse queryHitRecordListNewYear(){
        return APIResponse.returnSuccess(
                lotteryService.queryHitRecordListNewYear());
    }

    @RequestMapping(value = "/recordHitPrizeUserInfo")
    public APIResponse recordHitPrizeUserInfo(@RequestBody Map map , HttpServletRequest request){
        Integer uid = getUid(request, true);
        Integer rid = MapUtils.getInteger(map, "rid");
        String mobile = MapUtils.getString(map, "mobile");
        String address = MapUtils.getString(map, "address");
        if (rid == null || mobile == null || StringUtil.isBlankOrNull(mobile)){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        lotteryService.recordHitPrizeUserInfo(rid, uid, mobile, address);
        return APIResponse.returnSuccess();
    }
}
